package com.yx.cloud.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.iot.common.utils.LogUtils;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.bean.CreateDeviceParam;
import com.yx.cloud.databinding.ConnectsketchmapActivityBinding;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.WifiAdmin;
import com.yx.cloud.utils.WifiManager;

public class ConnectSketchMapActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "ConnectSketchMapActivity";

    private WifiManager wifiManager;
    private WifiInfo wifiInfo;
    private WifiAdmin wifiAdmin;
    private ConnectsketchmapActivityBinding binding;
    private CreateDeviceParam config_bean;
    private String robotType;

    @Override
    public void onCreate() {
        binding = ConnectsketchmapActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initData();
    }

    private String network;

    @SuppressLint("LongLogTag")
    @Override
    protected void onStart() {
        super.onStart();
        network = getIntent().getStringExtra("network");
        robotType = getIntent().getStringExtra("device_type");
        config_bean = (CreateDeviceParam) getIntent().getSerializableExtra("config_bean");
        Log.d(TAG, "ConnectSketchMapActivity  ----- onStart ： " + network);

        if (wifiManager == null) {
            wifiManager = new WifiManager(this);
            wifiAdmin = new WifiAdmin(this);
        }
        String checkState = wifiAdmin.checkState(this);
        if (checkState.equals("1")) {
            //wifiManager.openWifi();
            //ToastUtil.show(this, "Wifi网络未开启，请打开网络", Toast.LENGTH_LONG);
            return;
        }
//        Log.d(TAG, "ConnectSketchMapActivity  ----- wifiInfo :" + wifiInfo.getSSID());
    }

    private void initData() {
        binding.ivSketchMapReturn.setOnClickListener(this);
        binding.sketchMapBtnNext.setOnClickListener(this);
        binding.goToSettings.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        switch (v.getId()) {
            case R.id.iv_sketchMap_Return:
                Intent_utils.backIntent(this);
                break;
            case R.id.go_to_settings:
                Intent intent = new Intent();
                intent.setAction("android.net.wifi.PICK_WIFI_NETWORK");
                startActivityForResult(intent, 1);
                break;
            case R.id.sketchMap_btn_next:
                Intent intent_conn = new Intent(this, DeviceConnectActivity2.class);
                intent_conn.putExtra("network", network);
                startActivity(intent_conn);
                finish();
                break;
        }
    }

    private String intToIp(int i) {
        return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 24) & 0xFF);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            String ssid = wifiManager.getConnectedSSID().toUpperCase();
            int connectedWifiIp = wifiManager.getConnectedWifiIp();
            LogUtils.i(robotType+" : 当前连接网络 IP ：" + intToIp(connectedWifiIp));
            if (!TextUtils.isEmpty(ssid) && connectedWifiIp != 0) {
                if (ssid.contains("INXNI") || ssid.contains("ROBOT")) {
                    Intent intent = new Intent(this, DeviceConnectActivity2.class);
                    intent.putExtra("network", network);
                    intent.putExtra("config_bean", config_bean);
                    intent.putExtra("device_type", robotType);
                    startActivity(intent);
                    finish();
                } else {
                    //获取到连接的热点不是机器
                    //ToastUtil.show(ConnectSketchMapActivity.this, "WiFi not correct", Toast.LENGTH_SHORT);
                    binding.llConfirmConnect.setVisibility(View.VISIBLE);
                }
            } else {
                //没有获取连接热点的SSID
                //ToastUtil.show(ConnectSketchMapActivity.this, "Unable to get WiFi SSID", Toast.LENGTH_SHORT);
                // binding.goToSettings.setVisibility(View.GONE);
                binding.llConfirmConnect.setVisibility(View.VISIBLE);
            }

        }
    }

}
